package Net;

import Dialog.RepentanceDialog;
import MainBoard.ChessBoard;
import MainBoard.PVPChessBoard;
import MainBoard.PVPMainBoard;
import Model.VictoryJudgement;

import java.io.IOException;

/**
 * 落棋监听线程
 */
public class PlayChess implements Runnable {
    private PVPMainBoard mainBoard;
    private PVPChessBoard chessBoard;
    private NetContainer netContainer;
    private String message;
    private String[] messageSet;
    private int chessX;
    private int chessY;

    /**
     * 落棋监听的构造函数
     *
     * @param mainBoard    游戏界面
     * @param netContainer 客户端或服务端
     */
    public PlayChess(PVPMainBoard mainBoard, NetContainer netContainer) {
        this.mainBoard = mainBoard;
        this.netContainer = netContainer;
        this.chessBoard = mainBoard.getChessBoard();
    }

    @Override
    public void run() {
        try {
            message = netContainer.getIn().readUTF();
        } catch (IOException e) {
            e.printStackTrace();
        }
        messageSet = message.split("\\s+");
        if (messageSet[0].equals("POS")) {
            int winner;
            try {
                chessX = Integer.parseInt(messageSet[1]);
                chessY = Integer.parseInt(messageSet[2]);
            } catch (Exception e) {
                e.printStackTrace();
            }
            chessBoard.chessCoord[chessX][chessY] = chessBoard.getOpponentRole();
            chessBoard.saveStep(chessX, chessY);
            winner = VictoryJudgement.PPJudge(chessX, chessY,
                    chessBoard.chessCoord, chessBoard.getOpponentRole());
            chessBoard.overEvent(winner);
            if (chessBoard.getResult() == ChessBoard.GAME_START) {
                chessBoard.setClickable(true);
                mainBoard.getProcessLabel().setText("轮到你了");
            }
        } else if (messageSet[0].equals("Exit")) {
            mainBoard.getProcessLabel().setText("对方已退出游戏");
            mainBoard.getTimeThread().interrupt();
        } else if (messageSet[0].equals("Repentance")) {
            RepentanceDialog repentanceDialog = new RepentanceDialog(mainBoard, "悔棋", true, false);
            repentanceDialog.setVisible(true);
            try {
                if (repentanceDialog.getMessage() == RepentanceDialog.cancel) {
                    netContainer.getOut().writeUTF("Reject");
                } else {
                    netContainer.getOut().writeUTF("Agree");
                    chessBoard.backStep();
                }
                chessBoard.setClickable(false);
                new Thread(new PlayChess(mainBoard, netContainer)).start();
                mainBoard.getProcessLabel().setText("等待对方落棋");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        } else if (messageSet[0].equals("Reject")) {
            chessBoard.setClickable(true);
            mainBoard.getProcessLabel().setText("对方拒绝悔棋");
        } else if (messageSet[0].equals("Agree")) {
            chessBoard.setClickable(true);
            mainBoard.getProcessLabel().setText("对方同意悔棋");
            chessBoard.backStep();
        }
    }
}
